import time
from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy
#pip install Appium-Python-Client
from loguru import logger
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions

capabilities ={
  "platformName": "Android",
  "appium:platformVersion": "12.0",
  "appium:deviceName": "emulator-5554",
  "appium:appPackage": "com.tencent.wework",
  "appium:appActivity": "com.tencent.wework.launch.LaunchSplashActivity",
  "appium:noReset": True,
  "appium:dontStopAppOnReset": False,
  "appium:autoGrantPermissions": False,
  "appium:automationName": "UIAutomator2",
  "appium:adbExecTimeout": "80000",
  "forceAppLaunch": True
}


def get_faker_name():
    import faker
    logger.info('生成模拟数据-姓名')

    fake = faker.Faker(locale='zh_CN')  # 初始化，指定生成中文格式数据
    name = fake.name_female()  # 生成一条电话号码
    return name

def get_faker_number():
    import faker
    logger.info('生成模拟数据-手机号')

    fake = faker.Faker(locale='zh_CN')  # 初始化，指定生成中文格式数据
    phone = fake.phone_number()  # 生成一条电话号码
    return phone

appium_server_url = 'http://localhost:4723'
class Test_app_demo:
    def setup_class(self):
        self.driver = webdriver.Remote(appium_server_url, options=UiAutomator2Options().load_capabilities(capabilities))
        self.wait = WebDriverWait(self.driver,30)

    def find_ele(self,by,value):
        el = self.wait.until(expected_conditions.element_to_be_clickable((by, value)))
        return el

    def find_eles(self,by,value):
        self.find_ele(by,value)
        elements = self.driver.find_elements(by, value)
        return elements

    def find_ele_when_scroll(self,by,value):
        try:
            return self.driver.find_element(by=by,value=value)
        except Exception :
            self.driver.swipe(100,100,200,200,duration= 1000)
            self.find_ele_when_scroll(by,value)

    def tearDown_class(self):
        if self.driver:
            self.driver.quit()
    def test_demo_scroll(self):
        # 滑动操作--推荐使用该方法
        # time.sleep(10)
        # 查找通讯录
        el = self.wait.until(expected_conditions.element_to_be_clickable(
            (AppiumBy.XPATH, '//android.widget.TextView[@resource-id="com.tencent.wework:id/h1o" and @text="通讯录"]')))

        el.click()
        # time.sleep(2)
        # 添加成员
        # el = self.find_ele_when_scroll(AppiumBy.XPATH,
        #                                '//android.widget.TextView[@resource-id="com.tencent.wework:id/mid1Txt" and @text="添加成员"]')
        time.sleep(2)
        self.driver.swipe(300,300,100,100,duration=500)
        while 1:
            time.sleep(5)
            # 从屏幕四分之三的位置滑动到四分之一的位置
            self.driver.swipe(self.driver.get_window_size()['width'] / 2, self.driver.get_window_size()['height'] * 3 / 4, self.driver.get_window_size()['width'] / 2, self.driver.get_window_size()['height'] * 1 / 4, 2000) # 通过坐标进行滑动操作滑动时间为3秒
            time.sleep(5)
            self.driver.swipe(self.driver.get_window_size()['width'] / 2, self.driver.get_window_size()['height'] * 1 / 4,self.driver.get_window_size()['width'] / 2, self.driver.get_window_size()['height'] * 3 / 4,2000 ) # 通过坐标进行滑动操作滑动时间为3秒


    def test_demo(self):
        # time.sleep(10)
        #查找通讯录
        el = self.wait.until(expected_conditions.element_to_be_clickable((AppiumBy.XPATH, '//android.widget.TextView[@resource-id="com.tencent.wework:id/h1o" and @text="通讯录"]')))

        el.click()
        # time.sleep(2)
        #添加成员
        el =  self.find_ele_when_scroll(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.tencent.wework:id/mid1Txt" and @text="添加成员"]')
        # el = self.driver.find_element(by=AppiumBy.XPATH,value='(//android.widget.ImageView[@resource-id="com.tencent.wework:id/ivy"])[4]')
        el.click()
        # time.sleep(2)
        #手动添加输入
        el = self.find_ele(by=AppiumBy.XPATH,value='//android.widget.TextView[@text="手动输入添加"]')
        el.click()
        # time.sleep(2)
        #姓名输入框
        name_input = self.find_ele(by=AppiumBy.XPATH,value='//android.widget.EditText[@resource-id="com.tencent.wework:id/c8e"]')
        name_input.send_keys(get_faker_name())

        #手机输入框
        phone_input = self.find_ele(by=AppiumBy.XPATH,value='//android.widget.EditText[@resource-id="com.tencent.wework:id/iv9"]')
        phone_input.send_keys(get_faker_number())
        #点击保存
        el = self.find_ele(by=AppiumBy.XPATH,value='//android.widget.TextView[@resource-id="com.tencent.wework:id/b5d"]')

        el.click()
        time.sleep(3)
        #退格键有时会不生效，因为页面还未加载成功？是某些JavaScrip脚本导致的嘛？
        #而且貌似退格键不会退出页面，退格也是页面间的退格，而不是操作的退格。
        self.driver.keyevent(4)

        #点击工作台
        el = self.find_ele(by=AppiumBy.XPATH,value='//android.view.ViewGroup[@resource-id="com.tencent.wework:id/lf6"]/android.widget.RelativeLayout[4]/android.widget.RelativeLayout')
        el.click()

        # 点击管理企业
        el = self.find_ele(by=AppiumBy.ID,value='com.tencent.wework:id/h3g')
        el.click()

        #点击成员与部门管理
        el = self.find_ele(by=AppiumBy.XPATH,value='//android.widget.TextView[@resource-id="com.tencent.wework:id/c9s" and @text="成员与部门管理"]')
        el.click()

        #获取员工列表元素
        eles = self.find_eles(by=AppiumBy.XPATH,value='//androidx.recyclerview.widget.RecyclerView[@resource-id="com.tencent.wework:id/jm3"]//android.widget.TextView[@resource-id ="com.tencent.wework:id/geb" ]')

        for i in eles:
            print(i.text)


        # self.driver.back()

        #返回上一页面

        time.sleep(100)








